<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hotel_Facility_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
	/*
		insert or update facility
	*/
	public function save(){
		 $config['upload_path'] = './images/hotel-facility/';
		 $config['allowed_types'] = 'gif|jpg|png|jpge';
		 //$config['max_size'] = '100';
		 //$config['max_width'] = '48';
		 //$config['max_height'] = '48';

		 $this->load->library('upload', $config);
		 
		if(isset($_POST['facility_id'])&& $_POST['facility_id']=='0'){//insert new hotel
			$sql			= "INSERT INTO hotel_facility() VALUES()";
			$query 			= $this->db->query($sql); 
			$facility_id	= $this->db->insert_id(); 
			$this->update_facility($facility_id);
			return array($facility_id,'i');   
		}elseif(isset($_POST['facility_id'])&& $_POST['facility_id']!='0'){//update existing hotel
			$facility_id = $_POST['facility_id'];
			$this->update_facility($facility_id);
			return array($facility_id,'u');
		}else{
			return 'unknown';
		} 
	}	
	
	/*
		update facility information
	*/
	private function update_facility($facility_id=0){
		if($facility_id!=0){ 
			$is_enabled    = (isset($_POST['is_enabled'])?($_POST['is_enabled']=='1'?1:0):0); 
			$upload_result = $this->upload->do_upload('facility_logo');
			 
			if(!$upload_result){ 
				$sql="UPDATE hotel_facility SET 
					  name='".mysql_escape_string($_POST['name'])."', 
					  description= '".mysql_escape_string($_POST['description'])."',  
					  is_enabled=".$is_enabled." 
				  WHERE hotel_facility.id=".$facility_id;  
			}else{ 
				$upload_data = $this->upload->data();
				
				$sql="UPDATE hotel_facility SET 
					  name='".mysql_escape_string($_POST['name'])."', 
					  description= '".mysql_escape_string($_POST['description'])."',
					  icon='".mysql_escape_string($upload_data['file_name'])."',  
					  is_enabled=".$is_enabled." 
				  WHERE hotel_facility.id=".$facility_id;  
			} 
			
			 $this->db->query($sql);
			return true;
		}
		return false;
	}
	
	/*
		select hotel details of a given facility
	*/
	public function load_facility_detail($facility_id){
		$sql="SELECT 
			  hotel_facility.`id`,
			  hotel_facility.`name`,
			  hotel_facility.description,
			  hotel_facility.icon, 
			  hotel_facility.is_enabled
			FROM  hotel_facility
			WHERE id=".$facility_id;
		$query = $this->db->query($sql); 
        return $query->result();
	}	
	/*
		Load facility list
	*/
	public function load_facility_list(){
			$sql="SELECT 
			  hotel_facility.`id`,
			  hotel_facility.`name`, 
			  hotel_facility.icon,
			  hotel_facility.is_enabled
			FROM  hotel_facility";
		$query = $this->db->query($sql); 
        return $query->result();
	}
	/*
		delete a specified facility 
	*/
	public function delete_facility($facility_id=0){
		if($facility_id!='' && $facility_id!=0){
			$sql="DELETE FROM hotel_facility WHERE hotel_facility.id=".$facility_id;	
			$this->db->query($sql);
			return true;
		}else{
			return false;	
		}
	}
}